package com.raising.modules.sys.controller;

import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;

import com.raising.modules.sys.entity.SLog;
import com.raising.modules.sys.service.SLogService;
import com.raising.utils.JStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 日志记录 控制器
 *
 * @author raising
 * @version 1.0
 */
@Controller
@RequestMapping("${adminPath}/slog")
public class LogController extends BaseController {

    @Autowired
    private SLogService logService;

    /**
     * 查看日志列表
     * @return
     */
    @RequiresPermissions("sys:slog:view")
    @RequestMapping("/list")
    public String list() {
        olog("进入系统日志管理");
        return "sys/log/list";
    }

    /**
     * 跳转到查看系统日志详情
     * @author chenenmin
     * @dateTime 2018-1-11 下午2:50
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions("sys:slog:view")
    @RequestMapping(value = "/showDetial",method = RequestMethod.GET)
    public String showDetial(String id,Model model) throws Exception{
        olog("查询系统日志详情数据","{id:"+id+"}");
        model.addAttribute("sLog",logService.get(id));
        return "sys/log/showDetial";
    }

    /**
     * 批量删除日志
     *
     * @param ids
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("sys:slog:edit")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(String[] ids, int pageNo, int pageSize,
                         RedirectAttributes redirectAttributes) {
        olog("批量删除系统日志","{ids:"+ids+"}");
        logService.deleteAll(ids);
        addMessage(redirectAttributes, "删除成功");
        return "redirect:" + adminPath + "/slog?pageNo=" + pageNo + "&pageSize=" + pageSize;
    }

    /**
     * 加载系统日志列表分页数据
     * @author chenenmin
     * @dateTime 2018-1-11 上午 10:07
     * @param sLog
     * @param page
     * @return
     */
    @RequiresPermissions("sys:slog:view")
    @RequestMapping(value = "/getSysLogPage.json",method = RequestMethod.POST)
    public @ResponseBody ResultVo getSysLogPage(SLog sLog, Page<SLog> page) throws Exception{
        page.setEntity(sLog);
        if (JStringUtils.isNotBlank(sLog.getOrderBy())){
            page.setOrderBy(sLog.getOrderBy());
        }else {
            page.setOrderBy("a.create_date desc");
        }
        return ResultVo.success().data(logService.getPage(page));
    }
}
